/*
 * Copyright 2007 Stéphane Brunner <stephane.brunner@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <QtGui>
#include <QtOpenGL>
#include <QDebug>
#include <QImage>

#include "video.h"
#include "video.moc"

#include <iostream>
using namespace std;
#include <sys/time.h>

#define IMAGE_WIDTH 720
#define IMAGE_HEIGHT 576

#define IMAGE_RATIO 1


GLVideo::GLVideo(int width, int height, QWidget *parent) :
   QGLWidget(parent)
{
    setFixedSize(width, height);
    setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);

    scale_context = sws_getContext(IMAGE_WIDTH, IMAGE_HEIGHT, PIX_FMT_RGB32,
        IMAGE_WIDTH/IMAGE_RATIO, IMAGE_HEIGHT/IMAGE_RATIO, PIX_FMT_RGB32, 0, NULL, NULL, NULL);
    avpicture_alloc(&resized_frame, PIX_FMT_RGB32, IMAGE_WIDTH/IMAGE_RATIO, IMAGE_HEIGHT/IMAGE_RATIO);

    texture = -1;
}

GLVideo::~GLVideo()
{
}

void GLVideo::initializeGL()
{
    glEnable(GL_TEXTURE_2D);
}

// 720x576 => 65ms
// rescale 2x => 20 ms
// rescale 3x => 6 ms
// rescale 4x => 5 ms
void GLVideo::setFrame(AVPicture *original_frame) {
  struct timeval start;
  gettimeofday(&start, NULL);

  makeCurrent(); // 0.02ms

  if (texture != -1) {
    deleteTexture(texture); //0.8ms
  }

//  avpicture_fill(&original_frame, (uint8_t*)frame, PIX_FMT_RGB24, IMAGE_WIDTH, IMAGE_HEIGHT);

//  sws_scale(scale_context, &frame, frame_stride, IMAGE_WIDTH/IMAGE_RATIO, IMAGE_HEIGHT/IMAGE_RATIO, &image, image_stride);
  //qDebug()<<scale_context->sliceDir<<
//  sws_scale(scale_context, original_frame->data, original_frame->linesize, 0, IMAGE_HEIGHT, resized_frame.data, resized_frame.linesize);
  img_convert( &resized_frame, PIX_FMT_RGB32, original_frame, PIX_FMT_RGB32, IMAGE_WIDTH/IMAGE_RATIO, IMAGE_HEIGHT/IMAGE_RATIO );
//  qDebug()<<original_frame.data[0][1];
//  qDebug()<<resized_frame.data[0][1];

  QImage im((uint8_t*)resized_frame.data[0], IMAGE_WIDTH/IMAGE_RATIO, IMAGE_HEIGHT/IMAGE_RATIO, QImage::Format_ARGB32); // 0.1ms
//  QImage im((uint8_t*)original_frame.data[0], IMAGE_WIDTH, IMAGE_HEIGHT, QImage::Format_ARGB32); // 0.1ms
  texture = bindTexture(im); //56ms

  updateGL(); // 6ms

  struct timeval end;
  gettimeofday(&end, NULL);
//  cerr<<(end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0<<endl;
}

void GLVideo::paintGL()
{
  if (texture != -1) {
    glBindTexture( GL_TEXTURE_2D, texture );

    /* Draw quad */
    glBegin (GL_QUADS);
      glTexCoord2f (1, 1);  glVertex2f (width(), height());
      glTexCoord2f (0, 1);  glVertex2f (0, height());
      glTexCoord2f (0, 0);  glVertex2f (0, 0);
      glTexCoord2f (1, 0);  glVertex2f (width(), 0);
    glEnd ();
   
   struct timeval end;
 }
  else {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  }
}

void GLVideo::resizeGL(int width, int height)
{
  gluOrtho2D(0, width, 0, height);
}
